package com.xiangyi.teachingsystem.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiangyi.teachingsystem.entity.dto.CourseUserDto;
import com.xiangyi.teachingsystem.entity.po.Course;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CourseMapper extends BaseMapper<Course> {
    @Select("SELECT c.id,c.course_name,uc.user_id\n" +
            "FROM course c\n" +
            "LEFT JOIN user_course uc\n" +
            "ON uc.course_id=c.id\n" +
            "WHERE c.course_name=#{courseName}\n" +
            "AND uc.user_id=#{userId}")
    Course getOneByCourseIdAndUserId(String courseName, Integer userId);

    @Select("SELECT c.id course_id,c.course_name,c.course_url,c.course_desc,c.sign,c.course_sub,c.course_major,uc.user_id teacher_id,u.`name` teacher_name,u.head_url teacher_head_url\n" +
            "FROM course c\n" +
            "LEFT JOIN\n" +
            "user_course uc\n" +
            "ON uc.course_id=c.id\n" +
            "LEFT JOIN\n" +
            "`user` u\n" +
            "ON u.id=uc.user_id\n" +
            "WHERE c.id=#{courseId}")
    CourseUserDto getInfoById(Integer courseId);

    @Select("SELECT c.*,uc.user_id,(\n" +
            "SELECT u.`name`\n" +
            "FROM `user` u\n" +
            "WHERE u.id=uc.user_id\n" +
            ") name\n" +
            "FROM user_course uc\n" +
            "RIGHT JOIN course c\n" +
            "ON c.id=uc.course_id")
    List<Course> courseList();
}
